package com.ddwl.gate.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.gate.service.DictionaryService;
import com.ddwl.schema.bo.base.BaseBo;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.DictionaryBo;
import com.ddwl.schema.bo.gate.DictionarySearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.gate.DictionaryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/28 17:16
 */
@Api(tags = "数据字典服务")
@RequestMapping("/dictionary")
@RestController
@RequiredArgsConstructor
public class DictionaryController extends BaseController {

    private final DictionaryService dictionaryService;

    @ApiOperation("查询标签字典数据")
    @GetMapping("/field")
    public RestVo<List<DictionaryVo>> getByFieldTag(@ApiParam(value = "字段标签", required = true) @RequestParam String tag) {
        return RestVo.ok(dictionaryService.getByFieldTag(tag));
    }

    @ApiOperation("更新字典数据")
    @PostMapping("/field/update")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo updateFields(@ApiParam(value = "字典列表", required = true) @RequestBody List<DictionaryBo> bos) {
        dictionaryService.updateFields(bos);
        return RestVo.ok();
    }

    @ApiOperation("保存字典数据")
    @PostMapping("/field/save")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo save(@ApiParam(value = "字典数据", required = true) @RequestBody DictionaryBo bo) {
        dictionaryService.save(bo);
        return RestVo.ok();
    }

    @ApiOperation("保存字典数据")
    @PostMapping("/field/enable")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo enable(@ApiParam(value = "字典数据", required = true) @Validated({BaseBo.Update.class}) @RequestBody DictionaryBo bo) {
        dictionaryService.enable(bo);
        return RestVo.ok();
    }

    @ApiOperation("获取字典配置")
    @GetMapping("/field/val")
    public RestVo<String> getSettingVal(@RequestParam String tag, @RequestParam String field) {
        return RestVo.ok(dictionaryService.getSettingVal(tag, field));
    }

    @ApiOperation("获取tag字典配置")
    @GetMapping("/field/tag")
    @Auth(required = false)
    public RestVo<Map<String, String>> getSettingByTag(@RequestParam String tag) {
        return RestVo.ok(dictionaryService.getSettingByTag(tag));
    }

    @ApiOperation("删除字典数据")
    @DeleteMapping("/field/delete")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo delete(@ApiParam(value = "字典数据", required = true) @RequestParam String id) {
        dictionaryService.delete(id);
        return RestVo.ok();
    }

    @ApiOperation("查询标签字典数据")
    @PostMapping("/page")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<DictionaryVo>> page(@RequestBody PageBo<DictionarySearchBo> pageBo) {
        return RestVo.ok(dictionaryService.page(pageBo));
    }
}
